package com.iiifi.kite.boot.locale;

import java.util.Locale;

import org.springframework.context.MessageSource;

import com.iiifi.kite.boot.utils.WebUtils;
import com.iiifi.kite.core.util.StringUtils;

import lombok.AllArgsConstructor;

/**
 * 国际化service
 *
 * @author kite@iiifi.com 花朝
 */
@AllArgsConstructor
public class LocaleMessageService {
    private MessageSource messageSource;

    /**
     * 获取单个国际化翻译值
     */
    public String get(String key) {
        return this.get(key, StringUtils.parseLocale(WebUtils.getLanguage()));
    }

    /**
     * 获取单个国际化翻译值
     */
    public String get(String key, Locale locale) {
        try {
            return messageSource.getMessage(key, null, locale);
        } catch (Exception e) {
            return null;
        }
    }
}
